package 计算位置x到y的最少步数P2.src.main;
import java.util.*;
import static java.lang.Math.abs;

public class Main {
    public static int solution(int xPosition, int yPosition) {
        // Please write your code here
        int Distance = abs(xPosition - yPosition);
        if (isOdd(Distance)) {
            return 2 * SumInverse((Distance + 1) / 2);
        } else
            return 2 * SumInverse(Distance / 2);

    }

    public static boolean isOdd(int n) {
        return n % 2 != 0;
    }

    public static boolean isEven(int n) {
        return n % 2 == 0;
    }

    public static int Factorial(int n) {
        return n == 1 ? 1 : n * Factorial(n - 1);
    }
    // 线性搜索

    public static int FactorialInverse(int n) {
        int x = 1;
        long factorial = 1;
        // Calculate factorial until it is greater than or equal to n
        while (factorial < n) {
            x++;
            factorial *= x;
        }
        // Check if the factorial equals n
        if (factorial == n) {
            return x;
        } else {
            return -1; // No exact match found
        }
    }

    public static int SumInverse(int sum) {
        // Calculate the discriminant
        int discriminant = 1 * 1 - 4 * 1 * (-2 * sum);

        // Check if the discriminant is non-negative and a perfect square
        if (discriminant >= 0 && Math.sqrt(discriminant) == Math.floor(Math.sqrt(discriminant))) {
            // Calculate n using the quadratic formula
            int n = (int) ((-1 + Math.sqrt(discriminant)) / (2 * 1));
            return n;
        } else {
            return -1; // No exact integer solution found
        }
    }

    public static void main(String[] args) {
        // You can add more test cases here
        System.out.println(solution(12, 6) == 4);
        System.out.println(solution(34, 45) == 6);
        System.out.println(solution(50, 30) == 8);

    }
}